package org.example.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.catalina.User;
import org.example.aop.UserLoginToken;
import org.example.config.UserContextConfig;
import org.example.error.BusinessException;
import org.example.error.EmBusinessError;
import org.example.response.CommonReturnType;
import org.example.service.OrderService;
import org.example.service.model.OrderModel;
import org.example.service.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author itmei
 * @Package org.example.controller
 * @Date 2025/6/2 21:24
 */
@Controller("order")
@RequestMapping("/order")
public class OrderController extends BaseController{
    @Autowired
    private OrderService orderService;
    @Autowired
    private HttpServletRequest httpServletRequest;
    //封装下单请求
    @UserLoginToken
    @RequestMapping(value = "/createorder",method = {RequestMethod.POST})
    @ResponseBody
    public CommonReturnType createOrder(@RequestParam(name = "itemId")Integer itemId,
                                        @RequestParam(name = "promoId",required = false)Integer promoId,
                                        @RequestParam(name="amount")Integer amount) throws BusinessException {
       //从userContext中获取登录用户信息
        UserModel userModel = UserContextConfig.getUserModel();
        if(userModel==null){
            throw new BusinessException(EmBusinessError.USER_NOT_LOGIN,"用户还不能下单");
        }
        OrderModel orderModel =orderService.createOrder(userModel.getId(),itemId,promoId,amount);
        return CommonReturnType.create(null);
    }

}
